package fr.upmc.mdoc.tp1.servlets;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.upmc.mdoc.tp1.domain.DAOContact;
import fr.upmc.mdoc.tp1.domain.DAOContactGroup;
import fr.upmc.mdoc.tp1.models.Address;
import fr.upmc.mdoc.tp1.models.Contact;
import fr.upmc.mdoc.tp1.models.ContactGroup;
import fr.upmc.mdoc.tp1.models.Entreprise;
import fr.upmc.mdoc.tp1.models.PhoneNumber;

/**
 * Servlet implementation class NewContact
 */
public class NewContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewContact() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		DAOContact dao = new DAOContact();
		String firstName = request.getParameter("fn");
		String lastName = request.getParameter("ln");
		String email = request.getParameter("email");

		Contact contact = new Contact(firstName, lastName, email);

		// L'adresse du contact
		Address adr = new Address();
		adr.setStreet(request.getParameter("street"));
		adr.setCity(request.getParameter("city"));
		adr.setCountry(request.getParameter("country"));
		adr.setZip(request.getParameter("zip"));

		contact.setAddress(adr);

		// Les téléphones du contact
		contact.getPhones().add(
				new PhoneNumber("fixe", request.getParameter("phoneFixe"),
						contact));
		contact.getPhones().add(
				new PhoneNumber("port", request.getParameter("phonePort"),
						contact));
		contact.getPhones().add(
				new PhoneNumber("bureau", request.getParameter("phoneOffice"),
						contact));

		// Les groupes que le contact appartiennent
		DAOContactGroup daog = new DAOContactGroup();
		List<ContactGroup> groups = daog.getGroups();
		String groupname = request.getParameter("group");
		
		Set<ContactGroup> books = new HashSet<ContactGroup>();
		
		for (ContactGroup group : groups) {
			if(group.getGroupName().equals(groupname)) {
				daog.updateGroup(group);
			}
			//books.add(group);
			else {
				// Il faut avoir la possibilité de créer un groupe
				contact.setBooks(books);
			}
		}
				
		// FIN groupe

		/* Entreprise */
		Contact entrp = new Entreprise();
		entrp.setFirstName("BNP");
		entrp.setLastName("Paribas");
		entrp.setEmail("contact@bnp-paribas.net");
		
		entrp.setAddress(adr);
		entrp.setBooks(books);
		entrp.getPhones().add(
				new PhoneNumber("fixe", request.getParameter("phoneFixe"), contact));
		
		
		if (dao.addContact(contact)) {

			request.getRequestDispatcher("pages/accueil.jsp").forward(request,
					response);
		}
	}
}
